import {ADD_TO_CART,ADD_COUNTER} from './mutation-types'

export default {
  //执行addcar函数
  addCart({
    state,
    commit
  },payload) {
    return new Promise((resolve) => {
      // 1.查找之前数组中是否有该数组
      let oldProduct = state.cartList.find(item => item.iid === payload.iid)
      // 2.判断oldProduct
      if (oldProduct) {
        commit(ADD_COUNTER, oldProduct)
        resolve()
      } else {
        payload.count = 1
        payload.checked = false
        commit(ADD_TO_CART, payload)
        resolve()
        }
    }) 
  }
}
